package com.xz.coursearrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.coursearrange.common.ServerResponse;
import com.xz.coursearrange.common.validation.AddGroup;
import com.xz.coursearrange.common.validation.UpdateGroup;
import com.xz.coursearrange.entity.ClassTask;
import com.xz.coursearrange.entity.request.ClassTaskDTO;
import com.xz.coursearrange.entity.request.NullClassRoomRequest;
import com.xz.coursearrange.entity.request.UpdateCourseRequest;
import com.xz.coursearrange.entity.request.selectionOptionalLessonRequest;
import com.xz.coursearrange.service.ClassTaskService;
import com.xz.coursearrange.service.CoursePlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 */
@RestController
@Validated
public class ClassTaskController {

    @Resource
    private ClassTaskService classTaskService;

    /**
     * 查询开课任务
     */
    @GetMapping("/classtask/{page}/{semester}")
    public ServerResponse queryClassTask(@PathVariable("page")
                                         @Min(value = 1, message = "页码必须大于等于1")
                                         Integer page,

                                         @PathVariable("semester")
                                         @NotBlank(message = "学期不能为空")
                                         String semester,
                                         @RequestParam(defaultValue = "10")
                                         @Min(value = 1, message = "每页数量必须大于0")
                                         @Max(value = 100, message = "每页最多100条")
                                         Integer limit) {

        LambdaQueryWrapper<ClassTask> wrapper = new LambdaQueryWrapper<ClassTask>().eq(ClassTask::getSemester, semester);
        IPage<ClassTask> ipage = classTaskService.page(new Page<>(page, limit), wrapper);
        return ipage != null ? ServerResponse.ofSuccess(ipage) : ServerResponse.ofError("查询开课任务失败！");
    }

    /**
     * 手动添加课程任务
     *
     * @param c
     * @return
     */
    @PostMapping("/addclasstask")
    public ServerResponse addClassTask(@RequestBody @Validated(AddGroup.class) ClassTaskDTO c) {
        ClassTask classTask = new ClassTask();
        BeanUtils.copyProperties(c, classTask);
        return classTaskService.save(classTask) ? ServerResponse.ofSuccess("添加课程任务成功") : ServerResponse.ofError("添加课程任务失败");
    }

    /**
     * 更新课程任务
     */
    @PostMapping("/modifyclasstask")
    public ServerResponse modifyClassTask(@RequestBody @Validated(UpdateGroup.class) ClassTaskDTO c) {
        ClassTask classTask = new ClassTask();
        BeanUtils.copyProperties(c, classTask);
        return classTaskService.updateById(classTask) ? ServerResponse.ofSuccess("更新成功") : ServerResponse.ofError("更新失败");
    }


    /**
     * 删除开课任务
     *
     * @param id
     */
    @DeleteMapping("/deleteclasstask/{id}")
    public ServerResponse deleteClassTask(@PathVariable("id")
                                          @NotNull(message = "ID不能为空")
                                          Integer id) {
        return classTaskService.removeById(id) ? ServerResponse.ofSuccess("删除成功") : ServerResponse.ofError("删除失败");
    }

    /**
     * 获得学期集合
     */
    @GetMapping("/semester")
    public ServerResponse queryAllSemester() {
        LambdaQueryWrapper<ClassTask> wrapper = new LambdaQueryWrapper<ClassTask>().select(ClassTask::getSemester).groupBy(ClassTask::getSemester);
        List<ClassTask> list = classTaskService.list(wrapper);

        Set<String> set = list.stream().map(ClassTask::getSemester).collect(Collectors.toSet());

        return ServerResponse.ofSuccess(set);
    }

    /**
     * 排课算法接口，传入学期开始去查对应学期的开课任务，进行排课，排完课程后添加到course_plan表
     */
    @PostMapping("/arrange/{semester}")
    public ServerResponse arrange(@PathVariable("semester")
                                  @NotBlank(message = "学期不能为空")
                                  String semester) {
        return classTaskService.classScheduling(semester);
    }
    /**
     * 查询空闲课表(查询老师，班级同时空闲的时间段,记得哪一节课要修改，后面修改课表)，根据教师编号，班级编号
     */
    @Autowired
    CoursePlanService coursePlanService;
    @PostMapping("/selectionOptionalLesson")
    public ServerResponse selectionOptionalLesson(@RequestBody @Valid selectionOptionalLessonRequest request) {
        return coursePlanService.selectionOptionalLessonByClassId(request.getClassId(), request.getTeacherId());
    }
    /**
     * 查询对应时间可以安排的教室
     */
    @PostMapping("/selectionClassRoomNull")
    public ServerResponse selectionClassRoomNull(@RequestBody @Valid NullClassRoomRequest classRoomRequest) {
        return coursePlanService.selectionClassRoomByGrade(classRoomRequest.getGrade(),classRoomRequest.getLessonPlan());
    }
    /**
     * 修改课表，需要教室编号，班级编号，哪一节课要修改
     */
    @PostMapping("/updateCourse")
    public  ServerResponse updateCourse(@RequestBody @Valid UpdateCourseRequest request){
        return coursePlanService.updateCourse(request);
    }
}

